package com.ruoyi.whzj.controller;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.whzj.domain.WhPayrecord;
import com.ruoyi.whzj.service.IWhPayrecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户购买记录Controller
 * 
 * @author ruoyi
 * @date 2020-10-30
 */
@Controller
@RequestMapping("/whzj/payrecord")
public class WhPayrecordController extends BaseController
{
    private String prefix = "whzj/payrecord";

    @Autowired
    private IWhPayrecordService whPayrecordService;

    @RequiresPermissions("whzj:payrecord:view")
    @GetMapping()
    public String payrecord()
    {
        return prefix + "/payrecord";
    }

    /**
     * 查询用户购买记录列表
     */
    @RequiresPermissions("whzj:payrecord:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WhPayrecord whPayrecord)
    {
        startPage();
        whPayrecord.setStatus("1");
        List<WhPayrecord> list = whPayrecordService.selectWhPayrecordList(whPayrecord);
        return getDataTable(list);
    }

    /**
     * 导出用户购买记录列表
     */
    @RequiresPermissions("whzj:payrecord:export")
    @Log(title = "用户购买记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WhPayrecord whPayrecord)
    {
        List<WhPayrecord> list = whPayrecordService.selectWhPayrecordList(whPayrecord);
        ExcelUtil<WhPayrecord> util = new ExcelUtil<WhPayrecord>(WhPayrecord.class);
        return util.exportExcel(list, "payrecord");
    }

    /**
     * 新增用户购买记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户购买记录
     */
    @RequiresPermissions("whzj:payrecord:add")
    @Log(title = "用户购买记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WhPayrecord whPayrecord)
    {
        return toAjax(whPayrecordService.insertWhPayrecord(whPayrecord));
    }

    /**
     * 修改用户购买记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        WhPayrecord whPayrecord = whPayrecordService.selectWhPayrecordById(id);
        mmap.put("whPayrecord", whPayrecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户购买记录
     */
    @RequiresPermissions("whzj:payrecord:edit")
    @Log(title = "用户购买记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WhPayrecord whPayrecord)
    {
        return toAjax(whPayrecordService.updateWhPayrecord(whPayrecord));
    }

    /**
     * 删除用户购买记录
     */
    @RequiresPermissions("whzj:payrecord:remove")
    @Log(title = "用户购买记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(whPayrecordService.deleteWhPayrecordByIds(ids));
    }

    /**
     * 查询汇总
     */
    @RequiresPermissions("whzj:payrecord:list")
    @PostMapping("/getCount")
    @ResponseBody
    public AjaxResult getCount(String userName, String packageName,String payType,String payMethod) throws ParseException {
        Map<String,Object> resultMap = new HashMap<>();
        WhPayrecord whPayrecord = new WhPayrecord();
        whPayrecord.setUserName(userName);
        whPayrecord.setPackageName(packageName);
        whPayrecord.setPayType(payType);
        whPayrecord.setPayMethod(payMethod);
        whPayrecord.setStatus("1");
        List<WhPayrecord> list = whPayrecordService.selectWhPayrecordList(whPayrecord);
        BigDecimal sumMoney = new BigDecimal("0.0");
        for(WhPayrecord temp:list){
            sumMoney = sumMoney.add(temp.getPayMoney());
        }

        resultMap.put("sumMoney",sumMoney);
        return  new AjaxResult(AjaxResult.Type.SUCCESS,"200",resultMap);
    }
}
